package com.ysy.tz.core.utils;

import com.github.pagehelper.PageInfo;
import com.ysy.tz.core.engine.ApplicationContext;
import java.io.File;
import java.lang.reflect.Method;
import java.util.List;
import javafx.scene.control.TableView;
import javafx.stage.FileChooser;

public class ExcelUtils {
    private static int memoryEveryObject = 20480;
    private static double useFreeMemoryPercentage = 0.75;

    public ExcelUtils() {
    }

    public static void setMemoryStrategy(int memoryEveryObject, double useFreeMemoryPercentage) {
        ExcelUtils.memoryEveryObject = memoryEveryObject;
        ExcelUtils.useFreeMemoryPercentage = useFreeMemoryPercentage;
    }

    public static void export(TableView tableView, int totalCount, Object sum, Object serviceObj, String methodName, Object param) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle(ApplicationContext.commonBundle.getString("excel_out"));
        File file = fileChooser.showSaveDialog(ApplicationContext.mainStage);
        System.out.println("free memory --->>> " + Runtime.getRuntime().freeMemory() / 1024L / 1024L + "," + Runtime.getRuntime().totalMemory() / 1024L / 1024L);
        if (file != null) {
            String path = file.getPath();
            if (path.indexOf("xls") == -1) {
                path = path + ".xls";
            }

            int pageSize = (int)((double)Runtime.getRuntime().freeMemory() * useFreeMemoryPercentage / (double)memoryEveryObject);
            int pageNum = 1;
            int totalPage = (int)Math.ceil((double)totalCount * 1.0 / (double)pageSize);

            for(int i = 0; i < totalPage; ++i) {
                try {
                    Method setPageNum = null;
                    Method setPageSize = null;
                    Method[] methods = param.getClass().getMethods();
                    Method[] var16 = methods;
                    int var17 = methods.length;

                    for(int var18 = 0; var18 < var17; ++var18) {
                        Method method = var16[var18];
                        if (method.getName().equals("setPageNum")) {
                            setPageNum = method;
                        }

                        if (method.getName().equals("setPageSize")) {
                            setPageSize = method;
                        }
                    }

                    setPageNum.invoke(param, pageNum);
                    setPageSize.invoke(param, pageSize);
                    PageInfo result = (PageInfo)serviceObj.getClass().getMethod(methodName).invoke(serviceObj, param);
                    List resultList = result.getList();
                    ++pageNum;
                } catch (Exception var20) {
                    var20.printStackTrace();
                }
            }
        }

    }
}
